package com.linlong.linlongchat.service.impl;

import com.linlong.linlongchat.domain.dto.FriendDto;
import com.linlong.linlongchat.domain.po.Friend;
import com.linlong.linlongchat.mapper.FriendMapper;
import com.linlong.linlongchat.service.IFriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linlong.linlongchat.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 华少
 * @since 2024-09-20
 */
@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements IFriendService {
    @Autowired
    private FriendMapper friendMapper;

    @Override
    public List<Friend> getFriendList(String userAccount) {
        return friendMapper.getFriendList(userAccount);
    }

    @Override
    public boolean addFriend(FriendDto friendDto) {

        Map<String, Object> claims = ThreadLocalUtil.get();
        String userAccount = (String) claims.get("account");
        // 查询是否已经是好友
        Friend existfriend = friendMapper.selectFriend(userAccount, friendDto.getFriendAccount());
        if (existfriend != null) {
            return false; // 已经是好友
        }

        // 插入当前用户的好友关系
        Friend friend = new Friend();
        friend.setUserAccount(userAccount);
        friend.setFriendAccount(friendDto.getFriendAccount());
        friend.setRemark(friendDto.getRemark());
        friend.setCreateTime(LocalDateTime.now());
        friend.setUpdateTime(LocalDateTime.now());
        friend.setDeleted(0);

        int row = friendMapper.insert(friend);

        // 插入好友的好友关系
        Friend friend1 = new Friend();
        friend1.setUserAccount(friendDto.getFriendAccount());
        friend1.setFriendAccount(userAccount);
        friend1.setCreateTime(LocalDateTime.now());
        friend1.setUpdateTime(LocalDateTime.now());
        friend1.setDeleted(0);
        int row1 = friendMapper.insert(friend1);

        return row > 0 && row1 > 0;
    }

    @Override
    public Boolean findFriend(String userAccount, String friendAccount) {
        int count = friendMapper.findFriend(userAccount, friendAccount);
        if (count > 0) {    //是好友关系
            return true;
        }
        return false;
    }

    @Override
    public void updateRemark(FriendDto friendDto) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userAccount = (String) claims.get("account");
        friendMapper.updateRemark(userAccount, friendDto.getFriendAccount(), friendDto.getRemark());
    }

    @Override
    public void deleteFriend(String friendAccount) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        String userAccount = (String) claims.get("account");
        friendMapper.deleteFriend(userAccount, friendAccount);
        friendMapper.deleteFriend(friendAccount, userAccount);
    }
}
